/*  Keyboard.java
 *  CS 322
 *  
 *  Sep 7, 2012
 */

package urnoted;

import java.awt.*;
import static java.awt.Color.*;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

/** An instance represents a piano keyboard from C to C.
 */
public class StaffPanel extends JPanel{
    static final int staffWidth = 48*8;
    static final int staffOffset = 50;
    static final int lineHeight = 5;
    
    private Controller controller;
    
    private int x, y;  // position (upper left corner coordinates) of keyboard

    private Rectangle mLineRect;

    private Graphics2D g2;
    Timer timer;
    
    public StaffPanel(int x, int y,Controller control) {
    	controller = control;
        Dimension mySize = new Dimension(400,300);
        this.setPreferredSize(mySize);
        
        this.x = x;
        this.y = y + staffOffset;
        this.mLineRect = new Rectangle(this.x, this.y, staffWidth, lineHeight);
    }
    
    @Override
    public void paintComponent(Graphics g) {
    	g.clearRect(0, 0, 400, 300);
        this.g2 = (Graphics2D)g;
        System.out.println("StaffPanel paintComponent called");
        this.g2.setStroke(new BasicStroke(4));
        this.g2.setColor(Color.BLACK);
        if(controller.mMostRecentNote != null)
        {
        	if(controller.mMostRecentIsCorrect)
        	{
        		controller.generateNewNote();
        		repaint();
        	}
        	else
        	{
        		this.g2.setColor(Color.RED);
                for (int i = 0; i < 6; i++) {
                	//+15 for each subsequent
                    g2.draw(new Ellipse2D.Double(200+2*i, this.y + controller.mMostRecentNote.getmPosition(), 40-4*i, 30));
                }
        		timer = new Timer();
        		timer.schedule(new PaintTask(controller.mMostRecentNote), 3000);
        	}
        	controller.mMostRecentNote = null;
        }
        else
        {   
            for (int i = 0; i < 6; i++) {
                g2.draw(new Ellipse2D.Double(200+2*i, this.y + controller.getCurrentNote().getmPosition() , 40-4*i, 30));
            }
        }

        this.g2.setColor(Color.BLACK);
        this.mLineRect.y = this.y+30;
        for(int i = 0; i < 5; i++){

            this.g2.fill(this.mLineRect);
            this.mLineRect.y = this.mLineRect.y+30;
        }
    }
    
    class PaintTask extends TimerTask
    {
    	private Note mNoteToRemove;
    	
    	public PaintTask(Note noteToRemove)
    	{
    		mNoteToRemove = noteToRemove;
    	}
    	public void run()
    	{
    		System.out.println("Run called in PaintTask");
    		//g2.setColor(Color.BLACK);
    		//for (int i = 0; i < 6; i++) {
            //    g2.draw(new Ellipse2D.Double(200+2*i, y, 40-4*i, 30));
            //}
    		repaint();
    		timer.cancel();
    	}
    }
}